#include "task_config_dto.h"

namespace kernel {
namespace task {

// ==================== SubTaskConfigDTO ====================

std::string SubTaskConfigDTO::validate() const
{
    switch (type) {
        case SubTaskType::Move:
            if (stationName.empty()) {
                return "Move task: stationName is required";
            }
            if (floor < 1) {
                return "Move task: invalid floor number";
            }
            break;

        case SubTaskType::Jack:
            // operationStation可选（可在当前位置执行）
            if (waitTimeSeconds < 0) {
                return "Jack task: waitTimeSeconds must be >= 0";
            }
            break;

        case SubTaskType::Lift:
            if (liftNum <= 0) {
                return "Lift task: invalid liftNum";
            }
            if (callFloor < 1 || aimFloor < 1) {
                return "Lift task: invalid floor number";
            }
            if (callFloor == aimFloor) {
                return "Lift task: callFloor and aimFloor must be different";
            }
            break;

        case SubTaskType::Charge:
            if (targetBatteryLevel < 1 || targetBatteryLevel > 100) {
                return "Charge task: targetBatteryLevel must be 1-100";
            }
            break;

        case SubTaskType::Park:
            // parkingStation可选（自动选择）
            break;

        case SubTaskType::Unknown:
            return "Unknown task type";

        default:
            return "Invalid task type";
    }

    return ""; // 有效
}

// ==================== TaskTemplateConfigDTO ====================

std::string TaskTemplateConfigDTO::validate() const
{
    if (name.empty()) {
        return "Task template: name is required";
    }

    if (subTasks.empty()) {
        return "Task template '" + name + "': at least one subtask required";
    }

    // 验证所有子任务
    for (size_t i = 0; i < subTasks.size(); ++i) {
        std::string error = subTasks[i].validate();
        if (!error.empty()) {
            return "Task template '" + name + "' subtask[" +
                   std::to_string(i) + "]: " + error;
        }
    }

    // 验证允许的车型列表
    for (const auto& agvType : allowedAgvTypes) {
        if (agvType != agv::AgvType::MKLM && agvType != agv::AgvType::SEER) {
            return "Task template '" + name + "': invalid AgvType in allowedAgvTypes";
        }
    }

    return ""; // 有效
}

// ==================== TaskSystemConfigDTO ====================

const TaskTemplateConfigDTO* TaskSystemConfigDTO::findTemplate(const std::string& name) const
{
    for (const auto& tmpl : templates) {
        if (tmpl.name == name) {
            return &tmpl;
        }
    }
    return nullptr;
}

std::string TaskSystemConfigDTO::validate() const
{
    if (templates.empty()) {
        return "Task system config: no templates defined";
    }

    // 检查任务名称唯一性
    for (size_t i = 0; i < templates.size(); ++i) {
        for (size_t j = i + 1; j < templates.size(); ++j) {
            if (templates[i].name == templates[j].name) {
                return "Task system config: duplicate template name '" +
                       templates[i].name + "'";
            }
        }
    }

    // 验证所有模板
    for (const auto& tmpl : templates) {
        std::string error = tmpl.validate();
        if (!error.empty()) {
            return error;
        }

        // 验证互斥任务是否存在
        if (!tmpl.mutexTask.empty()) {
            if (findTemplate(tmpl.mutexTask) == nullptr) {
                return "Task template '" + tmpl.name +
                       "': mutex_task '" + tmpl.mutexTask + "' not found";
            }
        }
    }

    return ""; // 有效
}

} // namespace task
} // namespace kernel
